package com.zhanghe.study.springmvc.exception;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * @author zh
 * @date 2021/1/9 18:19
 */
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler
    @ResponseBody
    public String exception(Exception e) {
        return "全局捕获: 出现异常" + e.getMessage();
    }

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<Object> busExce(BusinessException e, WebRequest request) {

        return handleExceptionInternal(e, "出现业务异常", new HttpHeaders(), HttpStatus.EXPECTATION_FAILED, request);
    }
}
